<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class coursesComponents extends sfComponents {
	
	public function executeAbout() {
		if($this->course == null) {
			$this->course = CoursePeer::retrieveById($this->course_id);	
		}		
	}
	
	public function executeList() {
		$this->group_course_pager=GroupCoursePeer::retrieveByGroupIdPager($this->group_id,$this->page);
		$group_member = GroupMemberPeer::retrieveByUserIdAndGroupId($this->getUser()->getId(), $this->group_id);
		$this->group_display = (!is_null($group_member) ?  ($group_member->isAdmin() && $this->group_display) : false); //Whether user is admin
		$course_list = array();
		foreach($this->group_course_pager->getResults() as $key => $group_course ){
			$course_list[] = $group_course->getCourse(); 
		}
		$this->course_list = $course_list;
		
	
	}
	

	public function executeSelectbox(){
		if(isset($this->course_id)) {
			$this->id = $this->course_id;
		}
		
		if($this->subject_id)
		{
			$this->courses = CoursePeer::retrieveBySubjectId($this->subject_id);
		}
		else
		{
			$this->courses = CoursePeer::doSelect(new Criteria());
		}
	}
	
	public function executeProfileList(){
		
		$follow_courses = FollowCoursePeer::retrieveByUserId($this->user_id);
		
		//segment the answer by semeste
		$semester_array = array();
		foreach($follow_courses as $follow_course)
		{
			//get the course form the follow Course object
			$course = $follow_course->getCourse();
			
			
			//put each unique semester into array
			$semester = CourseSemesterPeer::retrieveByCourseId($course->getId());
			$semester = SemesterPeer::retrieveById($semester->getSemesterId());
			$semester = $semester->getTerm().' '.$semester->getYear('Y');
			
			if(isset($semester_array[$semester]))
			{
				$semester_array[$semester][] = $course;
		
			}
			else
			{
				$semester_array[$semester] = array();
				$semester_array[$semester][] = $course;
			}
		}
		
		$this->semesters = $semester_array;
	}
	
}